package com.woniu.bank.service.impl;

import com.github.pagehelper.PageHelper;
import com.woniu.bank.dto.GroupDTO;
import com.woniu.bank.dto.GroupMemberDefaultDataDTO;
import com.woniu.bank.dto.NewGroupDTO;
import com.woniu.bank.entity.Agreement;
import com.woniu.bank.mapper.AgreementMapper;
import com.woniu.bank.mapper.GroupMapper;
import com.woniu.bank.mapper.MemberMapper;
import com.woniu.bank.service.GroupService;
import com.woniu.bank.vo.GroupMemberDefaultDataVO;
import com.woniu.bank.vo.GroupVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author 晨星
 * @date 2023/01/04/14:35
 */
@Service
public class GroupServiceImpl implements GroupService {

    private final GroupMapper groupMapper;

    private final MemberMapper memberMapper;

    private final AgreementMapper agreementMapper;

    public GroupServiceImpl(GroupMapper groupMapper, MemberMapper memberMapper, AgreementMapper agreementMapper) {
        this.groupMapper = groupMapper;
        this.memberMapper = memberMapper;
        this.agreementMapper = agreementMapper;
    }

    @Override
    public List<GroupVO> conditionQuery(GroupDTO group) {
        PageHelper.startPage(group.getCurrentPage(), group.getPageSize());
        return groupMapper.conditionQuery(group);
    }

    @Override
    public String selectGroupLeaderName(String groupLeaderNo) {
        return memberMapper.selectGroupLeaderName(groupLeaderNo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int add(NewGroupDTO group) {
        //功能简化之后，导致写死数据-真实业务中有待优化
        group.setApprovalStatus("通过");
        int add = groupMapper.add(group);
        if (add == 1) {
            Agreement agreement = new Agreement();
            Date date = new Date();
            //小组编号为"NDXM"+当前时间毫秒值的拼接
            String agreementNo = "LBXY" + date.getTime();
            agreement.setAgreementNo(agreementNo);
            agreement.setGroupNo(group.getGroupNo());
            agreement.setAgreementStatus("未生效");
            agreementMapper.insertSelective(agreement);
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public int isDeleted(GroupVO group) {
        return groupMapper.isDeleted(group);
    }

    @Override
    public GroupMemberDefaultDataVO createDefaultData(List<GroupMemberDefaultDataDTO> members) {
        Date date = new Date();
        //小组编号为"NDXM"+当前时间毫秒值的拼接
        String groupNo = "NDXM" + date.getTime();
        GroupMemberDefaultDataVO defaultDataVO = new GroupMemberDefaultDataVO();
        defaultDataVO.setGroupNo(groupNo);
        defaultDataVO.setMembersNum(members.size());
        int groupLimit = 0;
        Set<String> noSet = new HashSet<>();
        for (GroupMemberDefaultDataDTO everyMember : members) {
            groupLimit += everyMember.getClientCreditLimit();
            noSet.add(everyMember.getMemberCustomerNo());
            memberMapper.updateGroupNo(groupNo, everyMember.getMemberId());
        }
        defaultDataVO.setGroupLimit(groupLimit);
        defaultDataVO.setTotalMargin((int) (groupLimit * 0.1));
        defaultDataVO.setGroupMemberNoSet(noSet);
        return defaultDataVO;
    }

    @Override
    public void quit(List<String> groupMemberNoList) {
        for (String no : groupMemberNoList) {
            memberMapper.updateGroupNo2(no);
        }
    }

}
